part of '../repository.dart';

const _kAccessCredentialsAccessTokenKey = 'access_token';
const _kAccessCredentialsRefreshTokenKey = 'refresh_token';

class _AccessCredentialsDto extends Equatable {
  const _AccessCredentialsDto._internal({
    required this.accessTokenDto,
    required this.refreshToken,
  });

  final _AccessTokenDto accessTokenDto;
  final String refreshToken;

  @override
  List<Object?> get props {
    return [
      accessTokenDto,
      refreshToken,
    ];
  }

  factory _AccessCredentialsDto.fromJson(Map<String, dynamic> json) {
    final accessTokenJson =
        Map.castFrom<dynamic, dynamic, String, dynamic>(json[_kAccessCredentialsAccessTokenKey]);
    final accessTokenDto = _AccessTokenDto.fromJson(accessTokenJson);

    return _AccessCredentialsDto._internal(
      accessTokenDto: accessTokenDto,
      refreshToken: json[_kAccessCredentialsRefreshTokenKey],
    );
  }

  factory _AccessCredentialsDto.fromEntity(AccessCredentials accessCredentials) {
    final accessTokenDto = _AccessTokenDto.fromEntity(accessCredentials.accessToken);

    return _AccessCredentialsDto._internal(
      accessTokenDto: accessTokenDto,
      refreshToken: accessCredentials.refreshToken,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kAccessCredentialsAccessTokenKey: accessTokenDto.toJson(),
      _kAccessCredentialsRefreshTokenKey: refreshToken,
    };
  }

  AccessCredentials toEntity() {
    return AccessCredentials(
      accessToken: accessTokenDto.toEntity(),
      refreshToken: refreshToken,
    );
  }
}
